package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.SysLogDao;
import com.itheima.dao.SysLogDao;
import com.itheima.domain.SysLog;
import com.itheima.factory.DaoInstanceFactory;
import com.itheima.service.SysLogService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.List;

public class SysLogServiceImpl implements SysLogService {

    @Override
    public PageInfo findAll(int page, int size) {
        SysLogDao dao = DaoInstanceFactory.getMapper(SysLogDao.class);
        PageHelper.startPage(page, size);
        List<SysLog> list = dao.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public void delete(String[] idArr) {
        SysLogDao dao = DaoInstanceFactory.getMapper(SysLogDao.class);
        dao.delete(idArr);
    }

    @Override
    public PageInfo<SysLog> search(String searchTime, String page, String size) {
        SysLogDao dao = DaoInstanceFactory.getMapper(SysLogDao.class);
        Integer currentPage = 1;
        try {
            currentPage = Integer.parseInt(page);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        Integer pageSize = 10;
        try {
            pageSize = Integer.parseInt(size);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        PageHelper.startPage(currentPage, pageSize);
        List<SysLog> sysLogs = dao.searchTime(searchTime);
        PageInfo<SysLog> pageInfo = new PageInfo<>(sysLogs);
        return pageInfo;
    }

    @Override
    public void getReport(ServletOutputStream sos) throws IOException {
        SysLogDao dao = DaoInstanceFactory.getMapper(SysLogDao.class);
        List<SysLog> list = dao.findAll();

        Workbook wb = new XSSFWorkbook();
        Sheet s = wb.createSheet("用户日志文件");

        //设置通用配置
        CellStyle cs_field = wb.createCellStyle();
        cs_field.setAlignment(HorizontalAlignment.CENTER);
        cs_field.setBorderTop(BorderStyle.THIN);
        cs_field.setBorderBottom(BorderStyle.THIN);
        cs_field.setBorderLeft(BorderStyle.THIN);
        cs_field.setBorderRight(BorderStyle.THIN);

        //制作标题
        s.addMergedRegion(new CellRangeAddress(1, 1, 1, 7));

        Row row_1 = s.createRow(1);
        Cell cell_1_1 = row_1.createCell(1);
        cell_1_1.setCellValue("用户访问日志信息表");

        //创建一个样式
        CellStyle cs_title = wb.createCellStyle();
        cs_title.setAlignment(HorizontalAlignment.CENTER);
        cs_title.setVerticalAlignment(VerticalAlignment.CENTER);
        cell_1_1.setCellStyle(cs_title);

        //制作表头
        String[] fields = {"日志数据ID", "用户访问时间", "访问用户名称", "用户IP地址", "访问路径url", "用户执行时间", "用户操作方法"};
        Row row_2 = s.createRow(2);

        for (int i = 0; i < fields.length; i++) {
            Cell cell_2_temp = row_2.createCell(1 + i);
            cell_2_temp.setCellValue(fields[i]);
            cell_2_temp.setCellStyle(cs_field);
        }
        //制作数据区
        int row_index = 0;
        for (SysLog ss : list) {    //sheet.autoSizeColumn(2, true);todo

            int cell_index = 0;
            Row row_temp = s.createRow(3 + row_index++);

            Cell cell_data_1 = row_temp.createCell(1 + cell_index++);
            cell_data_1.setCellValue(ss.getId());
            cell_data_1.setCellStyle(cs_field);

            Cell cell_data_2 = row_temp.createCell(1 + cell_index++);
            cell_data_2.setCellValue(ss.getVisitTime());
            cell_data_2.setCellStyle(cs_field);

            Cell cell_data_3 = row_temp.createCell(1 + cell_index++);
            cell_data_3.setCellValue(ss.getUsername());
            cell_data_3.setCellStyle(cs_field);

            Cell cell_data_4 = row_temp.createCell(1 + cell_index++);
            cell_data_4.setCellValue(ss.getIp());
            cell_data_4.setCellStyle(cs_field);

            Cell cell_data_5 = row_temp.createCell(1 + cell_index++);
            cell_data_5.setCellValue(ss.getUrl());
            cell_data_5.setCellStyle(cs_field);

            Cell cell_data_6 = row_temp.createCell(1 + cell_index++);
            cell_data_6.setCellValue(ss.getExecutionTime());
            cell_data_6.setCellStyle(cs_field);

            Cell cell_data_7 = row_temp.createCell(1 + cell_index++);
            cell_data_7.setCellValue(ss.getMethod());
            cell_data_7.setCellStyle(cs_field);

        }
        //将内存中的workbook数据写入到流中
        wb.write(sos);
        wb.close();
    }

    @Override
    public void record(SysLog sysLog) {
        SysLogDao dao = DaoInstanceFactory.getMapper(SysLogDao.class);
        dao.record(sysLog);
    }
}
